"""
File for the menu.
"""

import tkinter as tk
import tkinter.messagebox as messagebox
import webbrowser as wb
import sys

class MenuBar(tk.Menu):
    """Create the menu bar."""
    def __init__(self,master=None):
        self.verboseMode = tk.BooleanVar()
        self.verboseMode.set(True)

        tk.Menu.__init__(self, master)
        self.parent = master

        fileMenu = tk.Menu(self, tearoff=False)
        self.add_cascade(label='File', underline=0, menu=fileMenu)
        fileMenu.add_command(label='Exit', underline=1, command=lambda:sys.exit(0))

        optionMenu = tk.Menu(self, tearoff=False)
        self.add_cascade(label='Verbose', underline=0, menu=optionMenu)
        optionMenu.add_checkbutton(label='Verbose mode', underline=1, offvalue=False, variable=self.verboseMode, command=self.update_verbose_mode)
        optionMenu.add_separator()
        optionMenu.add_command(label='Help', underline=1, command=self.help_verbose_mode)

        helpMenu = tk.Menu(self, tearoff=False)
        self.add_cascade(label='Help', underline=0, menu=helpMenu)
        helpMenu.add_command(label='What is CRC?', underline=1, command=self.crc_info)
        helpMenu.add_command(label='About', underline=1, command=self.about)

    def update_verbose_mode(self):
        """Call when verbose mode is update (enabled or disabled)."""
        mainFrame = self.parent.mFrame
        mainFrame.resultFrame.requestEarlyExit = True
        if not (self.verboseMode.get() and mainFrame.inputFrame.verboseModeAllowed):
            mainFrame.calculationDetailsFrame.grid_remove();

    def help_verbose_mode(self):
        """Display the help about verbose mode."""
        text = '''Enable it to display all steps of the CRC calculation.

Note that the verbose mode is automatically disabled if
the polynomial has a degree over 16, because of
display problems.

It is aso automatically disabled when calculating a
file's CRC, because files are likely to be quite big,
and it would be too slow in verbose mode, even if
autostep is enabled.'''
        messagebox.showinfo('Verbose mode', text)

    def crc_info(self):
        """Open information about CRC in the web browser."""
        wb.open_new('http://en.wikipedia.org/wiki/Cyclic_redundancy_check')

    def about(self):
        """Display the dialog with an information message."""
        info = """Made by:

Clement Bellot,Remi Dubois,Florent Hemmi.

This program is under the MIT License.
See the license.txt file, or read it at
http://www.opensource.org/licenses/mit-license.php"""
        messagebox.showinfo('About', info)
